﻿
function Get-HttpResponse() {
    [Cmdletbinding()]
    param
    (
        [Parameter(Mandatory=$true)]
        [string] $Url,
        [switch] $IgnoreCertificateWarnings
    )

    Write-Verbose "$Url"

    $xhttp = New-Object -ComObject "MSXML2.ServerXMLHTTP.6.0";
    $xhttp.setTimeouts(1000, 1000, 1000, 10000);
    
    if ($IgnoreCertificateWarnings) {
        $xhttp.SetOption(2, 13056); # ignore certificate warnings
    }
    
    try
    {
        $xhttp.open("GET", $Url, $false);
        $xhttp.send();

        $status 	 	= $xhttp.status
        $statusText 	= $xhttp.statusText
    }
    catch
    {
        $status 	 	= -1
        $statusText 	= $_.Exception.Message
    }

    Write-Verbose "$status - $statusText"

    New-Object -TypeName "PSCustomObject" -Property @{
        Status      = [int] $status
        StatusTest  = [string] $statusText
        Url         = [string] $Url
    }
}

function Assert-HttpResponse {
    param (
        [Parameter(Mandatory=$true)]
        [string] $Url,
        [Parameter(Mandatory=$true)]
        [int] $expected,

        [switch] $IgnoreCertificateWarnings
    )

    # act
    $actual = Get-HttpResponse -Url $Url -IgnoreCertificateWarnings:$IgnoreCertificateWarnings

    # assert
    Assert-AreEqual -Expected $expected -Actual $actual.Status `
        -Message $Url
}
